package com.jtjrenren.android.taxi.driver.core.order.pay;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jtjrenren.android.taxi.driver.Constants;
import com.jtjrenren.android.taxi.driver.base.BaseActivity;
import com.jtjrenren.android.taxi.driver.bean.Order;
import com.jtjrenren.android.taxi.driver.bean.control.CmdCommonResponse;
import com.jtjrenren.android.taxi.driver.bean.event.ControlCenterEvent;
import com.jtjrenren.android.taxi.driver.manger.AppManger;
import com.jtjrenren.android.taxi.driver.manger.TtsManger;
import com.jtjrenren.android.taxi.driver.manger.push.cmd.ControlCmd;
import com.jtjrenren.android.taxi.driver.manger.push.cmd.Protocl;
import com.jtjrenren.android.taxi.driver.utils.ModuleUtils;
import com.jtjrenren.android.taxi.driver.R;
import com.jtjrenren.android.taxi.driver.bean.api.ApiOrderCost;
import com.jtjrenren.android.taxi.driver.manger.api.ApiWorks;
import com.library.utils.LogUtil;
import com.library.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by Administrator on 2016/5/9.
 */
public class OrderCommitPayActivity extends BaseActivity {

    @BindView(R.id.order_feespecar_fydetails_total)
    TextView tv_spefeedetails_total;
    @BindView(R.id.cost_desc)
    LinearLayout ll_cost;
    @BindView(R.id.order_feespecar_fydetails_total_input)
    EditText et_total;

    Order order;
    private ApiOrderCost.DataListEntity cost;
    private double total, total_f;

    public static Intent getDiyIntent(Context context, Order order) {
        Intent intent = new Intent(context, OrderCommitPayActivity.class);
        intent.putExtra("order", order);
        return intent;
    }

    @Override
    public int getLayoutID() {
        return R.layout.aty_commitpay;
    }

    @Override
    public void init() {
        EventBus.getDefault().register(this);


        if (getIntent().hasExtra("order"))
            order = (Order) getIntent().getExtras().get("order");

        ((TextView) findViewById(R.id.title_title)).setText("提交支付");
        findViewById(R.id.title_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        getOrderCost();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick(R.id.order_feespecar_fydetails_commit)
    public void clickCommit() {
        if (order == null || cost == null)
            return;

        if (cost.getTotalMoney() <= 0) {
            cost.setTotalMoney(0);
            Toast.makeText(this, "输入金额不能为0", Toast.LENGTH_SHORT).show();
            return;
        }

        String trim = et_total.getText().toString().trim();
        trim = StringUtils.isEmpty(trim) ? "0" : trim;
        double v = Double.parseDouble(trim);
        if (total_f > v) {
            cost.setTotalMoney(total_f);
            displayShort("金额不能小于原始金额");
            et_total.setText(total_f + "");
            return;
        }

        double xf = Double.parseDouble(StringUtils.isEmpty(order.getTip()) ? "0" : order.getTip());
        String info = cost.getFeeDesc();

        String commitCost = ControlCmd.getCommitCost(order.getOrderID(), Constants.TYPE_PAY_TJCK,
                ModuleUtils.getTjFyTime(Double.parseDouble(cost.getTimeLong())),
                ModuleUtils.formatPoint((float) cost.getDistance(), 1),
                ModuleUtils.formatPoint(cost.getTotalMoney(), 1) + "",
                ModuleUtils.formatPoint(xf, 1) + "", info, true);

        AppManger.getInstance().sendMsg(commitCost);

        showProDialog(null, getString(R.string.feiyongtijiaozhongqingshaohou));
    }

    @OnClick(R.id.specost_add)
    public void clickAdd() {
        et_total.setText((getMoney() + 1) + "");
    }

    @OnClick(R.id.specost_jian)
    public void clickJ() {
        double money = getMoney() - 1 > 0 ? (getMoney() - 1) : 0;
        double xf = Double.parseDouble(StringUtils.isEmpty(order.getTip()) ? "0" : order.getTip());
        if (money >= xf)
            et_total.setText(money + "");
    }

    @OnTextChanged(R.id.order_feespecar_fydetails_total_input)
    public void onTextChanged(CharSequence text) {
        String total = text.toString();
        total = StringUtils.isEmpty(total) ? "0" : total;
        double xf = Double.parseDouble(StringUtils.isEmpty(order.getTip()) ? "0" : order.getTip());
        double totals = Double.parseDouble(total);
        if (cost != null)
            cost.setTotalMoney(totals - xf);
    }

    private double getMoney() {
        String money = et_total.getText().toString().trim();
        money = StringUtils.isEmpty(money) ? "0" : money;
        return Double.parseDouble(money);
    }

    private void refreshView(ApiOrderCost.DataListEntity dataListEntity) {
        cost = dataListEntity;

        double xf = Double.parseDouble(StringUtils.isEmpty(order.getTip()) ? "0" : order.getTip());
        total = dataListEntity.getTotalMoney() + xf;
        total_f = total;

        et_total.setText(total_f + "");
        tv_spefeedetails_total.setText(total + "");

        List<LinearLayout> list = ModuleUtils.showEstimatedCost(this, dataListEntity.getFeeDesc());
        ll_cost.removeAllViews();
        for (LinearLayout ll : list)
            ll_cost.addView(ll);
    }

    private void getOrderCost() {
        if (order == null)
            return;
        ApiWorks.getOrderCost(order.getOrderID(), new ApiWorks.ResponseListener<ApiOrderCost>() {
            @Override
            public void onResponse(ApiOrderCost apiOrderCost) {
                if (apiOrderCost != null && apiOrderCost.getDataList() != null && apiOrderCost.getDataList().size() > 0) {
                    refreshView(apiOrderCost.getDataList().get(0));
                } else {
                    Toast.makeText(OrderCommitPayActivity.this, "数据异常，请稍后再试..", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Subscribe
    public void handControlEvent(ControlCenterEvent controlCenterEvent){
        String action = controlCenterEvent.getAction();
        if (Constants.ACTION_CONTROL_RESPONSE_COMMON.equals(action)) {
            disProDialog();
            CmdCommonResponse commonResponse = (CmdCommonResponse) controlCenterEvent.getResponse();
            LogUtil.log(OrderCommitPayActivity.class, "收到通用回复：" + commonResponse.toString());
            if ((commonResponse.getMsgID().equals(Protocl.CMD_SEND_TJFY) || commonResponse.getMsgID().equals(Protocl.CMD_SEND_TJFY_SPECAR)) && commonResponse.getResult() == 0) {
                disProDialog();
                TtsManger.getInstance().speak(getString(R.string.feiyongtijiaochegngong));
                setResult(RESULT_OK);
                finish();
            }
        }
    }
}
